function [Val_U,U]=Fun_U(PP,SS,Pir,dE,Lag_PortfolioInfo,VbarCoef,State,Flag_SS)
% State=SS.FunApp.U.State; Lag_PortfolioInfo=SS.PortfolioInfo;
if nargin<=7
    Flag_SS     =   0;
end
%% Preliminary
% States
N_State     =   size(State,1);
f           =   State(:,1);
LagID       =   State(:,2);

LagID_z     =   SS.FunApp.ExoState.State(LagID,1);
LagID_FI    =   SS.FunApp.ExoState.State(LagID,2);
LagID_RI    =   SS.FunApp.ExoState.State(LagID,3);


LagID_O     =   zeros(N_State,1);
for ii=1:size(SS.O_StateID,1)
    TempInd         =   (LagID_FI==SS.O_StateID(ii,1)) & (LagID_RI==SS.O_StateID(ii,2));
    LagID_O(TempInd)=   ii;
end

LagFI       =   PP.ExoState.Idio_FI.Node(LagID_FI);
Lag_PortfolioMat=reshape(Lag_PortfolioInfo,[4,2]);

Lag_DomShare_Pos=   Lag_PortfolioMat(1,1);
Lag_DomShare_Neg=   Lag_PortfolioMat(1,2);

DomShare    =   ones(N_State,1);
DomShare(LagFI & f>0)   =   Lag_DomShare_Pos;
DomShare(LagFI & f<0)   =   Lag_DomShare_Neg;
DomShare(LagFI & f==0)  =   PP.Fin_DomShare;

f           =   f.*(1+(1-DomShare)*dE*PP.Flag_dE)/(1+Pir);
%% Evaluation
BasMat      =   funbas(SS.FunApp.V.Space,f);

VbarCoef_Mat=   reshape(VbarCoef,SS.FunApp.V.MatSize_State);
VbarMat     =   BasMat*VbarCoef_Mat;

Cell_rr     =   cell(PP.ExoState.IdioInc.N,1);
Cell_cc     =   cell(PP.ExoState.IdioInc.N,1);
Cell_vv     =   cell(PP.ExoState.IdioInc.N,1);
Val_U       =   zeros(N_State,1);
for ii_z=1:PP.ExoState.IdioInc.N
    TempVbarMat         =   VbarMat(:,SS.O_StateIdx(ii_z,:));
    TempVbarMat         =   TempVbarMat - PP.O_AdjCost(:,LagID_O)';
    [TempTrProb,TempVmax] ...
                        =   Logit_Prob(0,PP.O_SIGMA,TempVbarMat);
    
    Val_U               =   Val_U + PP.ExoState.IdioInc.TrMat(ii_z,LagID_z)'.*TempVmax;
    
    Temp_rr             =   repmat((1:N_State)',[1,size(SS.O_StateIdx,2)]);
    Temp_cc             =   repmat(SS.O_StateIdx(ii_z,:),[N_State,1]);
    Temp_vv             =   PP.ExoState.IdioInc.TrMat(ii_z,LagID_z)' .* TempTrProb;
    
    Cell_rr{ii_z}       =   Temp_rr(:);
    Cell_cc{ii_z}       =   Temp_cc(:);
    Cell_vv{ii_z}       =   Temp_vv(:);
end

TrProbMat_zo=   sparse(cat(1,Cell_rr{:}),cat(1,Cell_cc{:}),cat(1,Cell_vv{:}),...
                       N_State,SS.FunApp.ExoState.N)';

U           =   struct('Val',Val_U,'TrProbMat_zo',TrProbMat_zo);

    